﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Student_management
{
    public partial class frmBCHocKy : Form
    {
        Connection db = new Connection();
        string ten, diachi, sdt, logo;
        float diemdau;
        public frmBCHocKy()
        {
            InitializeComponent();
        }

        private void frmBCHocKy_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            diemdau = float.Parse(db.getDS("Select GiaTri from QuyDinh where MaQuyDinh = 'DiemDat'").Rows[0][0].ToString());
            cboHocky.SelectedIndex = 0;

            DataTable a = new DataTable();
            a = db.getDS("Select * from ThongTin");
            ten = a.Rows[0]["Ten"].ToString();
            sdt = a.Rows[0]["DT"].ToString();
            diachi = a.Rows[0]["DiaChi"].ToString();
            logo = a.Rows[0]["Logo"].ToString();
            loaddata();
            this.reportViewer1.RefreshReport();
        }
        void loaddata()
        {
            DataTable ds = db.getDS("select MaLop as 'Lop' from Lop");
            ds.Columns.Add("Siso");
            ds.Columns.Add("STT");
            ds.Columns.Add("Soluong");
            ds.Columns.Add("Tile");
            for (int i = 0; i < ds.Rows.Count; i++)
            {
                ds.Rows[i]["STT"] = i + 1;
                ds.Rows[i]["Siso"] = db.getDS("Select count(*) from HocSinh where MaLop = '" + ds.Rows[i][0] + "'").Rows[0][0].ToString();
                ds.Rows[i][3] = CountDat(ds.Rows[i][0].ToString(), ds.Rows[i][1].ToString());
                if (ds.Rows[i][3].ToString() != "Chưa nhập đủ điểm" && ds.Rows[i][1].ToString() != "0")
                    ds.Rows[i][4] = float.Parse(ds.Rows[i][3].ToString()) / float.Parse(ds.Rows[i][1].ToString()) * 100 + " %";
                else
                    ds.Rows[i][4] = "Chưa nhập đủ điểm";
            }
            reportViewer1.Reset();
            reportViewer1.LocalReport.ReportEmbeddedResource = "Student_management.BCHK.rdlc";
            reportViewer1.LocalReport.DataSources.Clear();
            Microsoft.Reporting.WinForms.ReportDataSource newDataSource = new Microsoft.Reporting.WinForms.ReportDataSource("Baocaohk_BaoCao", ds);
            newDataSource.Name = "DataSet1";
            reportViewer1.LocalReport.DataSources.Add(newDataSource);
            reportViewer1.LocalReport.EnableExternalImages = true;
            List<Microsoft.Reporting.WinForms.ReportParameter> Parameters = new List<Microsoft.Reporting.WinForms.ReportParameter>();
            Microsoft.Reporting.WinForms.ReportParameter param = new Microsoft.Reporting.WinForms.ReportParameter("Hocky", cboHocky.Text);
            Parameters.Add(param);
            param = new Microsoft.Reporting.WinForms.ReportParameter("Ten", ten);
            Parameters.Add(param);
            param = new Microsoft.Reporting.WinForms.ReportParameter("SDT", sdt);
            Parameters.Add(param);
            param = new Microsoft.Reporting.WinForms.ReportParameter("Diachi", diachi);
            Parameters.Add(param);
            param = new Microsoft.Reporting.WinForms.ReportParameter("Logo", logo);
            Parameters.Add(param);
            reportViewer1.LocalReport.SetParameters(Parameters);
            reportViewer1.RefreshReport();
        }

        string CountDat(string MaLop, string siso)
        {
            if (db.getDS("select count(*) from Diem A join HocSinh B on A.MaHS = B.MaHS where MaLop='" + MaLop + "'").Rows[0][0].ToString() != siso)
                return "Chưa nhập đủ điểm";
            if (db.getDS("select * from Diem A join HocSinh B on A.MaHS = B.MaHS where MaLop='" + MaLop + "' and " + cboHocky.Text + " is null").Rows.Count > 0)
                return "Chưa nhập đủ điểm";
            DataTable a = db.getDS("select count(*) from Diem A join HocSinh B on A.MaHS = B.MaHS where MaLop='" + MaLop + "'and " + cboHocky.Text + " > '" + diemdau.ToString().Replace(",", ".") + "'");
            return a.Rows[0][0].ToString();
        }

        private void cboHocky_SelectedIndexChanged(object sender, EventArgs e)
        {
            loaddata();
        }
    }
}
